<template>
  <div class="addCluesWrap">
    <el-dialog title="新建线索1" :visible.sync="dialogVisible" width="50%">
      <div>
        <div class="section-header">
          <b class="section-mark"></b>
          <span>基本信息</span>
        </div>
        <el-form ref="crmForm" :model="fieldForm" :rules="fieldRules" :validate-on-rule-change="false" class="rp-form"
          inline label-position="top">
          <form-item-more v-for="(children, index) in columnList" :key="index" :field-from="fieldForm"
            :field-list="children" :ignore-fields="['leadsName']" @change="formChange">
            <template slot-scope="{ data }">
              <div v-if="data.field == 'leadsName'">
                <business-info-input v-if="canUseEnterprise" v-model="fieldForm[data.field]" :maxlength="100"
                  :disabled="data.disabled" :debounce="1000" :type="data.formType" value-key="name"
                  @select="customerNameSelect" @input="formChange(data, index, $event)" />
                <el-input v-else v-model.trim="fieldForm[data.field]" :disabled="data.disabled" maxlength="100"
                  :placeholder="data.placeholder" type="text" @input="formChange(data, index, $event)" />
                <el-button type="text" :disabled="!businessInfo || businessInfo.name !== fieldForm[data.field]"
                  class="premiumInfoBtn" data-type="BusinessInformation" @click="checkBIDetail">
                  <i class="rp rp-icon-lightning-solid rp-premium-info-icon" data-type="BusinessInformation" />
                  <span class="rp-premium-info-label" data-type="BusinessInformation">工商信息</span>
                </el-button>
              </div>
            </template>
          </form-item-more>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import FormItemMore from '@/components/busComponents/NewComponents/BusForm/FormItemMore'
import CustomFieldsMixin from '@/mixins/CustomFields'
import { queryField } from "@/api/customer/common";

export default {
  // 新建编辑
  name: 'LeadsCreate',

  components: {
    FormItemMore
  },

  mixins: [CustomFieldsMixin],
  props: {
    phone: String,
    addType: String,
    action: {
      type: Object,
      default: () => {
        return {
          type: 'save',
          id: '',
          data: {}
        }
      }
    }
  },

  data () {
    return {
      dialogVisible: false,
      loading: false,
      baseFields: [],
      columnList: [],
      fieldForm: {},
      fieldRules: {},

      // 工商信息
      businessInfo: null,
      businessInfoViewShow: false,
      canUseEnterprise: false
    }
  },
  mounted () {

  },
  methods: {
    queryField () {
      queryField(this.addType).then(res => {


      }).catch(error => {
        this.loading = false;
      });
    },
    queryListHead () {
      let res = {
        "code": 0,
        "msg": "success",
        "data": [
          [
            {
              "fieldId": "1666720787004305453",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "leadsName",
              "formType": "text",
              "name": "线索名称",
              "type": 1,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 1,
              "options": null,
              "fieldType": 1,
              "operating": 189,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 0,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.leadsName"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787004305455",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "source",
              "formType": "select",
              "name": "线索来源",
              "type": 3,
              "inputTips": null,
              "maxLength": null,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": "促销,搜索引擎,广告,转介绍,线上注册,线上询价,预约上门,陌拜,电话咨询,邮件咨询",
              "fieldType": 2,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [
                "促销",
                "搜索引擎",
                "广告",
                "转介绍",
                "线上注册",
                "线上询价",
                "预约上门",
                "陌拜",
                "电话咨询",
                "邮件咨询"
              ],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 1,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "0": "customField.leads.sourceOptions.促销",
                "1": "customField.leads.sourceOptions.搜索引擎",
                "2": "customField.leads.sourceOptions.广告",
                "3": "customField.leads.sourceOptions.转介绍",
                "4": "customField.leads.sourceOptions.线上注册",
                "5": "customField.leads.sourceOptions.线上询价",
                "6": "customField.leads.sourceOptions.预约上门",
                "7": "customField.leads.sourceOptions.陌拜",
                "8": "customField.leads.sourceOptions.电话咨询",
                "9": "customField.leads.sourceOptions.邮件咨询",
                "name_resourceKey": "customField.leads.source"
              },
              "xaxis": -1,
              "yaxis": -1
            },
          ],
          [
            {
              "fieldId": "1666720787008499713",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "ownerUserId",
              "formType": "user",
              "name": "负责人",
              "type": 10,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": [
                {
                  "userId": "1666720786639400960",
                  "img": null,
                  "realname": "admin",
                  "status": null,
                  "deptId": null,
                  "deptName": null,
                  "wxUserId": null,
                  "outerUserEmail": null
                }
              ],
              "isUnique": 0,
              "isNull": 1,
              "options": null,
              "fieldType": 1,
              "operating": 16,
              "companyId": "1666720786576486400",
              "setting": null,
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 2,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.ownerUserName"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787004305456",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "mobile",
              "formType": "mobile",
              "name": "手机",
              "type": 7,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 2,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.mobile"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787004305456",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "mobile",
              "formType": "mobile",
              "name": "手机",
              "type": 7,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 2,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.mobile"
              },
              "xaxis": -1,
              "yaxis": -1
            }
          ],
          [
            {
              "fieldId": "1666720787004305457",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "telephone",
              "formType": "text",
              "name": "电话",
              "type": 1,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 3,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.telephone"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787004305454",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "email",
              "formType": "email",
              "name": "邮箱",
              "type": 14,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 4,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.email"
              },
              "xaxis": -1,
              "yaxis": -1
            }
          ],
          [
            {
              "fieldId": "1666720787004305458",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "address",
              "formType": "position",
              "name": "地址",
              "type": 43,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": null,
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": null,
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 4,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": 1,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.address"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787004305459",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "industry",
              "formType": "select",
              "name": "客户行业",
              "type": 3,
              "inputTips": null,
              "maxLength": null,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": "IT,金融业,房地产,商业服务,运输/物流,生产,政府,文化传媒",
              "fieldType": 2,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [
                "IT",
                "金融业",
                "房地产",
                "商业服务",
                "运输/物流",
                "生产",
                "政府",
                "文化传媒"
              ],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 5,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "0": "customField.leads.industryOptions.IT",
                "1": "customField.leads.industryOptions.金融业",
                "2": "customField.leads.industryOptions.房地产",
                "3": "customField.leads.industryOptions.商业服务",
                "4": "customField.leads.industryOptions.运输/物流",
                "5": "customField.leads.industryOptions.生产",
                "6": "customField.leads.industryOptions.政府",
                "7": "customField.leads.industryOptions.文化传媒",
                "name_resourceKey": "customField.leads.industry"
              },
              "xaxis": -1,
              "yaxis": -1
            }
          ],
          [
            {
              "fieldId": "1666720787004305460",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "level",
              "formType": "select",
              "name": "客户级别",
              "type": 3,
              "inputTips": null,
              "maxLength": null,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": "A（重点客户）,B（普通客户）,C（非优先客户）",
              "fieldType": 2,
              "operating": 189,
              "companyId": "1666720786576486400",
              "setting": [
                "A（重点客户）",
                "B（普通客户）",
                "C（非优先客户）"
              ],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 6,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "0": "customField.leads.levelOptions.A（重点客户）",
                "1": "customField.leads.levelOptions.B（普通客户）",
                "2": "customField.leads.levelOptions.C（非优先客户）",
                "name_resourceKey": "customField.leads.level"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787004305461",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "nextTime",
              "formType": "datetime",
              "name": "下次联系时间",
              "type": 13,
              "inputTips": null,
              "maxLength": null,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 63,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 7,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.nextTime"
              },
              "xaxis": -1,
              "yaxis": -1
            }
          ],
          [
            {
              "fieldId": "1666720787004305462",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "remark",
              "formType": "textarea",
              "name": "备注",
              "type": 2,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": "",
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 1,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": null,
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 8,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.remark"
              },
              "xaxis": -1,
              "yaxis": -1
            },
            {
              "fieldId": "1666720787008499712",
              "parentFieldId": null,
              "label": 1,
              "fieldName": "tagZdybq",
              "formType": "field_tag",
              "name": "自定义标签",
              "type": 61,
              "inputTips": null,
              "maxLength": 255,
              "defaultValue": null,
              "isUnique": 0,
              "isNull": 0,
              "options": null,
              "fieldType": 0,
              "operating": 191,
              "companyId": "1666720786576486400",
              "setting": [],
              "authLevel": 3,
              "value": [],
              "sysInformation": null,
              "isOut": null,
              "isHidden": 0,
              "autoGeneNumber": null,
              "fieldDataId": null,
              "sorting": 9,
              "stylePercent": 50,
              "maxNumRestrict": null,
              "minNumRestrict": null,
              "precisions": null,
              "formPosition": null,
              "formAssistId": null,
              "optionsData": null,
              "fieldExtendList": null,
              "remark": null,
              "isPhotograph": null,
              "languageKeyMap": {
                "name_resourceKey": "customField.leads.tagZdybq"
              },
              "xaxis": -1,
              "yaxis": -1
            }
          ]
        ]
      }


      let list = res.data;

      const assistIds = this.getFormAssistIds(list)

      const baseFields = []
      const fieldList = []
      const fieldRules = {}
      const fieldForm = {}
      list.forEach(children => {
        const fields = []
        children.forEach(item => {
          const temp = this.getFormItemDefaultProperty(item)
          temp.show = !assistIds.includes(item.formAssistId)

          const canEdit = this.getItemIsCanEdit(item, this.action.type)
          // 是否能编辑权限
          if (temp.show && canEdit) {
            fieldRules[temp.field] = this.getRules(item)
          }

          // 是否可编辑
          temp.disabled = !canEdit

          // 特殊字段允许多选
          this.getItemRadio(item, temp)

          // 获取默认值
          if (temp.show) {
            fieldForm[temp.field] = this.getItemValue(item, this.action.data, this.action.type)
          }
          fields.push(temp)
          baseFields.push(item)
        })
        fieldList.push(fields)
      })

      this.baseFields = baseFields
      this.columnList = fieldList
      this.fieldForm = fieldForm
      this.fieldRules = fieldRules


    },

    /**
  * change
  */
    formChange (field, index, value, valueList) {
      if ([
        'select',
        'checkbox'
      ].includes(field.formType) &&
        field.remark === 'options_type' &&
        field.optionsData) {
        const { fieldForm, fieldRules } = this.getFormContentByOptionsChange(this.fieldList, this.fieldForm)
        this.fieldForm = fieldForm
        this.fieldRules = fieldRules
      }
    },
    customerNameSelect (item) {
      this.businessInfo = item
    },

    /**
     * @description: 查看工商详情
     * @param {*}
     * @return {*}
     */
    checkBIDetail () {
      if (!this.canUseEnterprise) return
      this.businessInfoViewShow = true
    }
  }
}
</script>
<style lang="scss" scoped>
// ::v-deep .section-header{
//   padding-left: 32px !important;
// }
// ::v-deep .el-dialog {
//   width: 80% !important;
// }
// ::v-deep .el-form-item {
//   width: 25% !important;
// }
.addCluesWrap {
  padding: 13px 32px 22px 32px;

  .section-header {
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    padding: 5px 0;

    .section-mark {
      height: 14px;
      border-left-style: solid;
      border-left-width: 4px;
      border-radius: 2px;
      border-left-color: rgb(35, 98, 251);
    }

    span {
      flex-shrink: 0;
      margin-left: 8px;
      font-size: 16px;
      font-weight: 500;
    }
  }

  >span {
    flex-shrink: 0;
    margin-left: 8px;
    font-size: 16px;
    font-weight: 500;
  }
}

.premiumInfoBtn {
  position: absolute;
  top: -30px;
  right: 0;
}
</style>